package cn.tramy.pinxian.common.exception

/**
 * Created by yanjunhua on 16/5/30.
 */
class CommonException(code: String, message: String) : BaseException(code, message) {

    constructor(message: String) : this(CommonErrorCodes.COMMON_COMMON_EXCEPTION, message)

    companion object {

        fun parameterError(): CommonException {
            return CommonException(CommonErrorCodes.COMMON_PARAMETER_ERROR, CommonErrorCodes.COMMON_MESSAGE_PARAMETER_ERROR)
        }

        fun parameterRequired(name: String): CommonException {
            return CommonException(CommonErrorCodes.COMMON_PARAMETER_REQUIRED, String.format(CommonErrorCodes.COMMON_MESSAGE_TEMPLATE_PARAMETER_REQUIRED, name))
        }

        fun invalidOperation(): CommonException {
            return CommonException(CommonErrorCodes.COMMON_OPERATION_INVALID, CommonErrorCodes.COMMON_MESSAGE_OPERATION_INVALID)
        }

        fun unknownException(): CommonException {
            return CommonException(CommonErrorCodes.COMMON_UNKNOWN_EXCEPTION, CommonErrorCodes.COMMON_MESSAGE_UNKNOWN_EXCEPTION)
        }

        fun nameDuplicatedException(name: String): CommonException {
            return CommonException(CommonErrorCodes.COMMON_NAME_DUPLICATED, String.format(CommonErrorCodes.COMMON_MESSAGE_TEMPLATE_NAME_DUPLICATED, name))

        }

        fun codeDuplicatedException(code: String): CommonException {
            return CommonException(CommonErrorCodes.COMMON_CODE_DUPLICATED, String.format(CommonErrorCodes.COMMON_MESSAGE_TEMPLATE_CODE_DUPLICATED, code))

        }

        fun commonException(msg: String): CommonException = CommonException(CommonErrorCodes.COMMON_COMMON_EXCEPTION, msg)


        fun NPE(): CommonException {
            return CommonException(CommonErrorCodes.COMMON_NPE, CommonErrorCodes.COMMON_MESSAGE_NPE)

        }
    }
}
